<?php

// +----------------------------------------------------------------------
// | 文件 ShareController.class.php
// +----------------------------------------------------------------------
// | 说明 分享后相关页面
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Common\Util\ThinkSDK\ThinkOauth;

class ShareController extends HomeController 
{
    public function _initialize() {
        parent::_initialize();
        C('DEFAULT_THEME', 'ui2');
    }
	public function register()
	{
        $this->redirect('user/register');
        $this->title = "账号注册";
		$this->display();
	}

	public function password()
	{
        $this->title = "重置密码";
		$this->display();
	}

    public function sorry()
    {
        $this->display();
    }

	public function video($id = 0)
	{
        if (!IS_AJAX) {
            setRedirect();
        }
        if (UID == 0){
            $mobilebrower = mobileBrower();
            $redirect_url = curPageURL();
            switch ($mobilebrower) {
                case 'sina':
                    $sns = ThinkOauth::getInstance('sina');
                    $sns->code($redirect_url);
                    break;
                case 'qq':
                    $sns = ThinkOauth::getInstance('qq');
                    $sns->code($redirect_url);
                    break;
                case 'weixin':
                    $sns = ThinkOauth::getInstance('weixin');
                    $sns->code($redirect_url);
                    break;    
                default:
                    break;
            }
        }

        if (!$id){
            $this->redirect('/');
        }

        $vEvent = event('video');
        $video = $vEvent->detail( $id, UID );

        if ($video['type'] == 1) {
            return $this->match($id);
        }

        if ( !$video || $video['status'] == -1) {
            $this->redirect('/');
        }

        event('history')->add( UID, $id );

        $map = [
            'game_id' => $video['game_id'],
            'id'      => ['neq', intval($id)]
        ];
        $video['tags'] && $map['tags'] = ['$in' => $video['tags']];

        /*
         * 推荐视频数据
         */
        $recommendPager = I('pager', 8, 'intval');
        $about_ids = $vEvent->randomLists($map, $recommendPager);
        $about_videos = [];
        foreach ($about_ids as $key => $id) {
            $about_videos[] = $vEvent->detail($id, UID);
        }

        /*
         * PC使用ajax方式请求时，返回推荐视频数据
         */
        if (IS_AJAX && !is_mobile_request()) {
            $this->ajaxReturn(array('state'=>0, 'info'=>'操作成功', 'data'=>$about_videos));
        }

        $video['user']['tradeCount'] = service('video')->where(['uid' => $video['user']['id']])->count();
        $this->video = $video;
        $this->about_videos = $about_videos;

        $this->title = $video['title'];
        $this->description = $video['description'];

        // 用于统计来自第三方合作站点的访问
        $this->appid = I('appid', 0, 'intval');

        if(is_mobile_request()){
            C('DEFAULT_THEME', 'mobile3');

            $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
            $isios = strpos($agent, 'iphone') || strpos($agent, 'ipad');
            $this->type = $isios ? 'ios' : 'android';

            $this->browser = strpos($agent, 'micromessenger') ? 'weixin' : 'normal';

            $template = "mobile";

            if ($this->type == 'ios' && (IS_WEIXIN || strpos($agent, 'Weibo'))) {
                // return $this->display('Public/weixin');
                $this->assign('is_weixin', 1);
            }

            $appRes = A('Api/AppVersion', 'Event')->getLatest($this->type);
            $this->assign('appRes', $appRes);
        } else {
            C('DEFAULT_THEME', 'v4');
        }
        $this->adCode = 'webvideoshare';
        $this->display();
	}

	public function barrage($id = 0)
	{
        $vEvent = A( 'Api/Video', 'Event' );
        $video = $vEvent->detail( $id, UID );
		$comments = event('Comments')->get($id, 300);

        $count = count($comments);

        $contents = array_column($comments, 'content');
        $video_time = $video['time'];

        foreach ($contents as $key => $content) {
            $time[] = floor($key * $count / $video_time);     
        }
        $res = [
            'ckplayer' => [
                $time, $contents
            ]
        ];
        $this->ajaxReturn($res);
	}

    /**
     * UcloudPlayer 弹幕
     * @author liubo <liubo@ilongyuan.com.cn>
     */
    public function barrageUcloud($id = 0) {
        $vEvent = A( 'Api/Video', 'Event' );
        $video = $vEvent->detail( $id, UID );
        $pager = I('pager', 300, 'intval');
        $comments = event('Comments')->get($id, $pager);
        $index = I('index', 0, 'intval');
        $list = [];
        $colorList = ["yello", "red", "orange", "white", "white", "white", "white", "white", "white", "white"];
        $list[] = count($comments);
        foreach ($comments as $value) {
            $list[] = [
                'msg' => $value['content'],
                'fontColor' => $index ? "0xFF0000" :"0xFFFFFF",//$colorList[rand(10, 0)-1],
                'fontSize' => 20,
                'fontMode' => 'roll',
                'time' => '0:00:05',
            ];
        }
        $this->ajaxReturn($list);
    }

    public function match($id = 0)
    {
        if(!IS_MOBILE) {
            // 重定向到新版赛事页 liubo 2016-7-28
            $this->redirect("/match-detail-{$id}");
        }
        setRedirect();
        if (UID == 0){
            $mobilebrower = mobileBrower();
            $redirect_url = curPageURL();
            switch ($mobilebrower) {
                case 'sina':
                    $sns = ThinkOauth::getInstance('sina');
                    $sns->code($redirect_url);
                    break;
                case 'qq':
                    $sns = ThinkOauth::getInstance('qq');
                    $sns->code($redirect_url);
                    break;
                case 'weixin':
                    $sns = ThinkOauth::getInstance('weixin');
                    $sns->code($redirect_url);
                    break;    
                default:
                    break;
            }
        }

        if (!$id){
            $this->redirect('Index/index_redirect');
        }

        $vEvent = A( 'Api/Video', 'Event' );
 
        $video = $vEvent->detail( $id, UID );
        if ( !$video || !isset($video['match_videos'])) {
            $this->redirect('Index/index_redirect');
        }
        $service = D('Common/Video', 'Service');
        if ($video['type'] == $service::TYPE_NORMAL) {
            $redirect_url = U('Home/share/video', array('id' => $id));
            $this->redirect($redirect_url);
        }

        A( 'Api/History', 'Event' )->add( UID, $id );

       
        $map = [
            'game_id' => $video['game_id'],
            'id'      => ['neq', $id]
        ];
        $video['tags'] && $map['tags'] = ['$in' => $video['tags']];
        $about_ids = $vEvent->randomLists($map, 4);
        $about_videos = [];
        foreach ($about_ids as $key => $id) {
            $about_videos[] = $vEvent->detail($id);
        }
         // dump($video);
        $this->assign('video', $video);
        $this->assign('about_videos', $about_videos);
        $this->title = $video['title'];
        $this->description = $video['description'];

        $androidRes = A('Api/AppVersion', 'Event')->getLatest('android');
        $this->assign('androidRes', $androidRes);
            
        if (IS_AJAX){
            header('Access-Control-Allow-Origin: *');
            foreach ($about_videos as $key => &$value) {
                $value['link'] = U('video', ['id' => $value['id'] ]);
            }
            header('Access-Control-Allow-Headers: X-Requested-With');
            $ret = compact('video', 'about_videos', 'androidRes');
            $this->ajaxReturn($ret);
        }

        // 用于统计来自第三方合作站点的访问
        $this->appid = I('appid', 0, 'intval');

        if(is_mobile_request()){
           C('DEFAULT_THEME', 'mobile');
           $this->display('_match');
           return;
        }
        C('DEFAULT_THEME', 'ui3');
        $this->assign('currentTab','match');
        $this->assign('title', $this->title);
        $this->display();
    }
}